package bancosys.tec.persist.fieldhistory;

import org.hibernate.event.PostInsertEvent;
import org.hibernate.event.PostInsertEventListener;
import org.hibernate.event.PostLoadEvent;
import org.hibernate.event.PostLoadEventListener;
import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PostUpdateEventListener;

/**
 * Filtro que limpa history das propriedades FieldHistory quando o bean é salvo.
 * 
 * @created Feb 6, 2007
 * @author Gustavo Almeida
 */
public class ClearHistoryEventListener implements PostInsertEventListener, PostUpdateEventListener, PostLoadEventListener {

    /**
     * Limpa o history do Fieldhistory.
     * 
     * @param event o evento do PostInsert.
     */
    public void onPostInsert(PostInsertEvent event) {
        Object bean = event.getEntity();
        if (bean != null && bean.getClass().isAnnotationPresent(FieldHistory.class)) {
            HistoryHolder.resetHistory(bean);
        }
    }

    /**
     * Limpa o history do Fieldhistory.
     * 
     * @param event o evento do PostUpdate.
     */
    public void onPostUpdate(PostUpdateEvent event) {
        Object bean = event.getEntity();
        if (bean != null && bean.getClass().isAnnotationPresent(FieldHistory.class)) {
            HistoryHolder.resetHistory(bean);
        }
    }

    /**
     * Limpa o history do Fieldhistory.
     * 
     * @param event o evento do PostLoad.
     */
    public void onPostLoad(PostLoadEvent event) {
        Object bean = event.getEntity();
        if (bean != null && bean.getClass().isAnnotationPresent(FieldHistory.class)) {
            HistoryHolder.resetHistory(bean);
        }
    }
}